package cn.study.vue_project_xxx.service.impl;

import cn.study.vue_project_xxx.common.enumeration.MapKey;
import cn.study.vue_project_xxx.common.exception.KnowException;
import cn.study.vue_project_xxx.common.constants.BaseConstants;
import cn.study.vue_project_xxx.entity.dto.RegisterUserDTO;
import cn.study.vue_project_xxx.entity.po.TAdmin;
import cn.study.vue_project_xxx.entity.po.UserInfo;
import cn.study.vue_project_xxx.manager.UserInfoManager;
import cn.study.vue_project_xxx.manager.UserLoginManager;
import cn.study.vue_project_xxx.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static cn.study.vue_project_xxx.common.enumeration.MapKey.userInfoKey;
import static cn.study.vue_project_xxx.common.enumeration.MapKey.userLoginKey;

@Service
public class UserServiceImpl implements UserService {
    /**
     * 获取 HashMap中的userInfoKey
     */
    private final String userInfoKey = "userInfo";
    /**
     * 获取 HashMap中的userLoginKey
     */
    private final String userLoginKey = "userLogin";
    /**
     * 获取 HashMap中的userInfoIdKey
     */
    private final String userInfoIdKey = "userInfoId";


    @Resource
    UserInfoManager userInfoManager;

    @Resource
    UserLoginManager userLoginManager;

    /**
     * 新增普通用户
     *
     * @param registerUserDTO 注册参数
     * @return Boolean
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addUser(RegisterUserDTO registerUserDTO) {
        HashMap<String, Integer> resultMap = userInfoManager.saveEntity(registerUserDTO, BaseConstants.BaseNumberTwo);
        // 返回保存结果
        return Objects.nonNull(resultMap.get(userInfoKey)) &&
                Objects.nonNull(resultMap.get(userLoginKey)) &&
                BaseConstants.BaseNumberTwo
                        .equals((resultMap.get(userInfoKey)+resultMap.get(userLoginKey)));
    }

    /**
     * 修改普通用户
     *
     * @param registerUserDTO 接受用户修改参数
     * @return Boolean
     */
    @Override
    public Boolean updateUser(UserInfo registerUserDTO) {
        //校验数据
        if (Objects.isNull(registerUserDTO)){
            throw new KnowException("参数缺失！");
        }
        if (registerUserDTO.getUserId()==null){
            throw new KnowException("无法获取登录者信息！");
        }
        HashMap<String, Integer> update = userInfoManager.update(registerUserDTO);

        return Objects.nonNull(update.get(userInfoKey)) &&
                update.get(userInfoKey).equals(BaseConstants.BaseNumberOne);
    }



    @Override
    public boolean deleteUser(List<Long> userIds) {
        //校验数据
        if (Objects.isNull(userIds)){
            throw new KnowException("无法得到要删除的数据！");
        }
        //执行删除
        HashMap<String, Integer> map = userInfoManager.deleteUser(userIds);
        return Objects.nonNull(map.get(userInfoKey)) &&
                Objects.nonNull(map.get(userLoginKey)) && map.get(userInfoKey)>=0;
    }

    @Override
    public Page<UserInfo> selectUsers(Page<UserInfo> page,List<Long> userIds) {
        //校验数据
        if (Objects.isNull(userIds)){
            throw new KnowException("无法得到要查询的数据！");
        }
        //执行查询
        return userInfoManager.selectUsers(page,userIds);
    }




}
